#pragma once

#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;

/*HJJ QQ479287006
 *
 * */
long long countSubarrays(vector<int> &nums, int minK, int maxK) {

    int size = nums.size() - 1;
    int low = 0;
    int high = 0;
    int minV = -1;
    int maxV = -1;
    int count = 0;


    for (int index = 0; index <= size; index++) {

        low = index;
        high = index;


        while (high <= size && nums[high] <= maxK) {

            if (nums[high] == minK) {
                minV = minK;
            }

            if (nums[high] == maxK) {
                maxV = maxK;
            }


            high++;

            if ((minV == minK) && (maxV == maxK)) {
                count++;
            }

        }


        //走到头了 low++  3 1 5 2 7 5
        while (low <= size && low <= high) {

            if (nums[low] == minK) {
                minV = -1;
            }
            if (nums[low] == maxK) {
                maxV = -1;
            }
            low++;

            if ((minV == minK) && (maxV == maxK)) {
                count++;
            }

        }

    }

    return count;
}